home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14689 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  52 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: RossBoylan@aol.com (Ross Boylan)
  3. Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc
  4. Subject: Virtual function question
  5. Date: Mon, 01 Apr 1996 18:17:56 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4jp6e9$ou5@dub-news-svc-1.compuserve.com>
  8. NNTP-Posting-Host: ad30-253.compuserve.com
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. I am trying to define, a la Smalltalk, a mix-in which defines the
  12. various comparison operators in terms of one fundamental operator, <.
  13.  
  14.  
  15. typedef int bool;
  16. class RBMagnitude {
  17. public:
  18. //subclass should redefine < with appropriate types
  19.     virtual bool operator<(const RBMagnitude&) const= 0;
  20.     virtual bool operator>(const RBMagnitude& x) const {return  *this <
  21. x;};
  22. // and many more
  23. };
  24.  
  25. class A : public RBMagnitude {
  26. public:
  27.     virtual bool operator<(const A& a) const {return (this->n < a.n);};
  28. private:
  29.     int n;
  30. };
  31.  
  32.  
  33. class A test;
  34.  
  35. This fails in MSVC++ 4.0 with the error that A is virtual because the
  36. operator<(const RBMagnitude&) is not defined.
  37. It looks to me as if this is because the operator< for A has a
  38. different type signature (it uses an A rather than an RBMagnitude).
  39. If so, the problem is the language definition, not Microsoft's
  40. implementation.
  41.  
  42. 1) Is this interpretation correct?
  43. 2) Is there any way around this problem?  Commenting out the
  44. definition of RBMagnitude::operator< doesn't work, because
  45. RBMagnitude::operator> requires it.  I suppose I could define a bogus
  46. operator< which throws an exception, but this seems awkward (in
  47. particular, if A and B are both RBMagnitudes and I ask A<B, I fail at
  48. run time rather than compile time).
  49.  
  50. I would appreciate e-mail; I'll summarize here.
  51.  
  52.